package gxwl.user.controller;

import gxwl.common.pojo.CommonResult;
import gxwl.common.pojo.CommonValidList;
import gxwl.relation.param.SysUserGrantResourceParam;
import gxwl.relation.param.SysUserGrantRoleParam;
import gxwl.user.service.SysUserService;
import gxwl.user.vo.AuthAccountPasswordLoginParam;
import gxwl.user.vo.SysUserAddParam;
import gxwl.user.vo.SysUserEditParam;
import gxwl.user.vo.SysUserIdParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.util.List;

@RestController
@Validated
@RequiredArgsConstructor
@RequestMapping("/gxwl/api/user")
public class Login {

    private final SysUserService sysUserService;

    /***
     * @description: 账号密码登录
     *
     * @author: baijhc
     * @date: 2024/3/19 14:17
     * @param: authAccountPasswordLoginParam
     **/
    @Operation(summary = "账号密码登录")
    @PostMapping("/login")
    public CommonResult<String> doLogin(@RequestBody @Valid AuthAccountPasswordLoginParam authAccountPasswordLoginParam) {
        return CommonResult.data(sysUserService.doLogin(authAccountPasswordLoginParam));
    }

    /**
     * @description: 添加用户
     *
     * @author: baijhc 
     * @date: 2024/3/21 11:18
     * @param: sysUserAddParam 
     * @return String
     **/
    @ApiOperation("添加用户")
    @PostMapping("/add")
    public CommonResult<String> add(@RequestBody @Valid SysUserAddParam sysUserAddParam) {
        sysUserService.add(sysUserAddParam);
        return CommonResult.ok();
    }

    /**
     * @description: 编辑用户
     *
     * @author: baijhc
     * @date: 2024/3/21 09:49
     * @param: sysUserEditParam
     * @return String
     **/
    @ApiOperation("编辑用户")
    @PostMapping("/edit")
    public CommonResult<String> edit(@RequestBody @Valid SysUserEditParam sysUserEditParam) {
        sysUserService.edit(sysUserEditParam);
        return CommonResult.ok();
    }

    /**
     * @description: 删除用户
     *
     * @author: baijhc 
     * @date: 2024/3/21 11:19 
     * @param: sysUserIdParamList 
     * @return String
     **/
    @ApiOperation("删除用户")
    @PostMapping("/delete")
    public CommonResult<String> delete(@RequestBody @Valid @NotEmpty(message = "集合不能为空")
                                               CommonValidList<SysUserIdParam> sysUserIdParamList) {
        sysUserService.delete(sysUserIdParamList);
        return CommonResult.ok();
    }

    /**
     * @description: 禁用用户
     *
     * @author: baijhc
     * @date: 2024/3/21 13:31
     * @param: sysUserIdParam
     * @return String
     **/
    @ApiOperation("禁用用户")
    @PostMapping("/disable")
    public CommonResult<String> disableUser(@RequestBody SysUserIdParam sysUserIdParam) {
        sysUserService.disableUser(sysUserIdParam);
        return CommonResult.ok();
    }


    /**
     * @description: 启用用户
     *
     * @author: baijhc
     * @date: 2024/3/21 13:31
     * @param: sysUserIdParam
     * @return String
     **/
    @ApiOperation("启用用户")
    @PostMapping("/enable")
    public CommonResult<String> enableUser(@RequestBody @Valid SysUserIdParam sysUserIdParam) {
        sysUserService.enableUser(sysUserIdParam);
        return CommonResult.ok();
    }

    /**
     * @description: 重置用户密码
     *
     * @author: baijhc
     * @date: 2024/3/21 13:33
     * @param: sysUserIdParam
     * @return String
     **/
    @ApiOperation("重置用户密码")
    @PostMapping("/reset-password")
    public CommonResult<String> resetPassword(@RequestBody @Valid SysUserIdParam sysUserIdParam) {
        sysUserService.resetPassword(sysUserIdParam);
        return CommonResult.ok();
    }

    /**
     * @description: 用户拥有角色
     *
     * @author: baijhc
     * @date: 2024/3/21 13:50
     * @param: sysUserIdParam
     * @return String
     **/
    @ApiOperation("获取用户拥有角色")
    @GetMapping("/own-role")
    public CommonResult<List<String>> ownRole(@Valid SysUserIdParam sysUserIdParam) {
        return CommonResult.data(sysUserService.ownRole(sysUserIdParam));
    }

    /**
     * @description: 给人员授权角色
     *
     * @author: baijhc
     * @date: 2024/3/21 14:26
     * @param: bizUserGrantRoleParam
     * @return String
     **/
    @ApiOperation("给用户授权角色")
    @PostMapping("/grant-role")
    public CommonResult<String> grantRole(@RequestBody @Valid SysUserGrantRoleParam sysUserGrantRoleParam) {
        sysUserService.grantRole(sysUserGrantRoleParam);
        return CommonResult.ok();
    }

    /**
     * @description: 给用户授权资源
     *
     * @author: baijhc
     * @date: 2024/3/21 14:52
     * @param: sysUserGrantResourceParam
     * @return String
     **/
    @ApiOperation("给用户授权资源")
    @PostMapping("/sys/user/grantResource")
    public CommonResult<String> grantResource(@RequestBody @Valid SysUserGrantResourceParam sysUserGrantResourceParam) {
        sysUserService.grantResource(sysUserGrantResourceParam);
        return CommonResult.ok();
    }

}
